<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工程编号" prop="projectId">
        <el-input
          v-model="queryParams.projectId"
          placeholder="请输入工程编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批状态" prop="checkSts">
        <el-select v-model="queryParams.checkSts" clearable placeholder="请选择审批状态" size="small">
          <el-option
            v-for="dict in dict.type.REPAIR004"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleUpdate"
        >审批拒绝</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >审批通过</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="repairCheckList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工程编号" align="center" prop="projectId" />
      <el-table-column label="工程名称" align="center" prop="projectName" />
      <el-table-column label="小区" align="center" prop="areaName" />
      <el-table-column label="楼宇" align="center" prop="groupName" />
      <el-table-column label="户" align="center" prop="doorId" />
      <el-table-column label="房屋编号" align="center" prop="houseId" />
      <el-table-column label="审批状态" align="center" prop="checkSts" />
      <el-table-column :formatter="numberFormat"  label="分摊金额" align="center" prop="repairMoney" />
      <el-table-column :formatter="numberFormat"  label="修改金额" align="center" prop="updateMoney" />
      <el-table-column label="修改时间" align="center" prop="updateDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="updater" />
      <el-table-column label="操作" align="center"  fixed="right"  class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >审批拒绝</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >审批通过</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改维修金额审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="房屋编号" prop="houseId">
          <el-input v-model="form.houseId" placeholder="请输入房屋编号" />
        </el-form-item>
        <el-form-item label="工程编号" prop="projectId">
          <el-input v-model="form.projectId" placeholder="请输入工程编号" />
        </el-form-item>
        <el-form-item label="审批状态">
          <el-select v-model="form.checkSts" placeholder="请输入审批状态">
            <el-option
              v-for="dict in dict.type.REPAIR004"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分摊金额" prop="repairMoney">
          <el-input v-model="form.repairMoney" placeholder="请输入分摊金额" />
        </el-form-item>
        <el-form-item label="修改金额" prop="updateMoney">
          <el-input v-model="form.updateMoney" placeholder="请输入修改金额" />
        </el-form-item>
        <el-form-item label="修改时间" prop="updateDate">
          <el-date-picker clearable size="small"
            v-model="form.updateDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择修改时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="修改人" prop="updater">
          <el-input v-model="form.updater" placeholder="请输入修改人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRepairCheck, getRepairCheck, delRepairCheck, addRepairCheck, updateRepairCheck } from "@/api/repair/repairCheck";

export default {
  name: "RepairCheck",
  dicts: ['REPAIR004'],
  data() {
    return {

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维修金额审核表格数据
      repairCheckList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        checkSts: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectId: [
          { required: true, message: "工程编号不能为空", trigger: "blur" }
        ],
        checkSts: [
          { required: true, message: "审批状态不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    numberFormat (row, column, cellValue) {
      cellValue += ''
      if (!cellValue.includes('.')) cellValue += '.'
      return cellValue.replace(/(\d)(?=(\d{3})+\.)/g, function ($0, $1) {
        return $1 + ','
      }).replace(/\.$/, '')
    },
    /** 查询维修金额审核列表 */
    getList() {
      this.loading = true;
      listRepairCheck(this.queryParams).then(response => {
        this.repairCheckList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        checkId: null,
        houseId: null,
        projectId: null,
        checkSts: null,
        repairMoney: null,
        updateMoney: null,
        updateDate: null,
        updater: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.checkId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const checkIds = row.checkId || this.ids;
      this.$modal.confirm('是否确认通过审核编号为"' + checkIds + '"的数据项？').then(function() {
        return updateRepairCheck(checkIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("审批拒绝");
      }).catch(() => {});
    },
    /** 审批通过按钮操作 */
    handleDelete(row) {
      const checkIds = row.checkId || this.ids;
      this.$modal.confirm('是否确认通过审核编号为"' + checkIds + '"的数据项？').then(function() {
        return delRepairCheck(checkIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("审批通过成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('repair/repairCheck/export', {
        ...this.queryParams
      }, `repairCheck_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
